package cn.oscar.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.oscar.mapper.UserMapper;
import cn.oscar.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import commonUtils.dto.AdminDTO;
import commonUtils.dto.UserDTO;
import commonUtils.entities.Administrator;
import commonUtils.entities.Restful;
import commonUtils.entities.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImp extends ServiceImpl<UserMapper,User> implements UserService  {

    @Autowired
    private UserMapper mapper;


    @Override
    public Restful<Page<UserDTO>> selectByPage(int page, int pageSize, String name) {
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //构造分页构造器
        Page<User> pageInfo = new Page<>(page,pageSize);
        Page<UserDTO> pageInfoDto = new Page<>();

        //构造分页查询
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件

        queryWrapper.like(name != null && !name.trim().equals(""),User::getNickname,name);

        //添加排序条件 order by 条件为最后的登录时间
        queryWrapper.orderByDesc(User::getLastLoginTime);

        //执行查询
        page(pageInfo,queryWrapper);

        BeanUtil.copyProperties(pageInfo,pageInfoDto,"records");

        List<User> records = pageInfo.getRecords();
        List<UserDTO> recordsDto = records.stream().map((user)->{
            UserDTO userDto = new UserDTO();
            BeanUtil.copyProperties(user,userDto);
            return userDto;
        }).collect(Collectors.toList());

        pageInfoDto.setRecords(recordsDto);

        return Restful.success(pageInfoDto);
    }

    @Override
    public Restful<String> deleteUser(Long[] ids) {
        log.info("进入删除方法");
        //物理删除
//        dishService.removeById(ids);
        //逻辑删除
        if (ids.length >0){
            for (Long id : ids) {
                User u = getById(id);
                if (u.getStatus() == 0){
                    u.setIsDeleted(1);
                    updateById(u);
                }else {
                    return Restful.error("逻辑删除失败");
                }
            }
            log.info("删除对象成功");
        }else{
            return Restful.error("用户状态修改失败");
        }
        return Restful.success("删除成功");
    }

    @Override
    public Restful<String> updateByUser(User user) {
        updateById(user);
        return Restful.success("修改用户成功");
    }

    @Override
    public Restful<String> saveUser(User user) {
        save(user);
        return Restful.success("添加用户成功");
    }

    @Override
    public Restful<User> selectById(Long id) {
        User user = getById(id);
        return Restful.success(user);
    }

    @Override
    public Restful<String> changeStatus21(Long[] ids) {
        log.info("进入到状态修改操作");
        if (ids.length >0){
            for (Long id : ids) {
                User u = getById(id);
                u.setStatus(1);
                updateById(u);
            }
            log.info("状态修改成功");
        }else{
            return Restful.error("用户状态修改失败");
        }
        return Restful.success("用户状态修改成功");
    }

    @Override
    public Restful<String> changeStatus20(Long[] ids) {
        log.info("进入到状态修改操作");
        if (ids.length >0){
            for (Long id : ids) {
                User u = getById(id);
                u.setStatus(0);
                updateById(u);
            }
            log.info("状态修改成功");
        }else{
            return Restful.error("用户状态修改失败");
        }
        return Restful.success("用户状态修改成功");
    }

}
